import { createRouter, createWebHashHistory, RouteRecordRaw ,createWebHistory} from 'vue-router'
import NotFundPage from '../views/404.vue'
import LayOut from '../Layout/index.vue'
import ThreeMenuRoute from '../components/MenuRoute.vue'
import { storageLocal, storageSession } from '@/storage/index'
import { warnMessage } from '@/utils/messageTips'
const routes: Array<RouteRecordRaw> = [
  // 首页、面板
  {
    path: '/',
    name: 'Welcom',
    component: LayOut,
    redirect: '/welcom/first',
    children: [
      {
        path: '/welcom/first',
        name: 'First',
        component: () => import('@/views/PageOne.vue'),
        meta: {
          title: '数据面板',
          showLink:true,
          icon:'DataBoard'
        }
      },
    ],
    meta: {
      title: '首页',
      showLink:true
    }
  },

  // 用户管理
  {
    path: '/user',
    component: LayOut,
    redirect: '/user/userinfo',
    children: [
      {
        path: '/user/userinfo',
        name: 'UserInfo',
        component: () => import('@/views/UserInfo.vue'),
        meta: {
          title: '用户信息',
          showLink:true,
        
        }
      },
      {
        path: '/user/applylist',
        name: 'UserConfig',
        component: () => import('@/views/ApplyList.vue'),
        meta: {
          title: '申请记录',
          showLink:true,
          
        }
      },
    ],
    meta: {
      title: "用户管理",
      showLink:true,
      icon:'UserFilled'
    }
  },

  // 商品管理

  {
    path: '/goods',
    component: LayOut,
    redirect: '/goods/goodsinfo',
    children: [

      {
        path: '/goods/goodsinfo',
        name: 'GoodsInfo',
        component: () => import('@/views/GoodsInfo.vue'),
        meta: {
          title: '商品信息',
          showLink:true,
          icon:'goodsFilled'
        }
      },
    ],
    meta: {
      title: "商品管理",
      showLink:true,
     
    }
  },

  // 订单管理
  {
    path: '/order',
    component: LayOut,
    redirect: '/order/orderlist',
    children: [
      {
        path: '/order/orderlist',
        name: 'OrderList',
        component: () => import('@/views/OrderList.vue'),
        meta: {
          title: '订单信息',
          showLink:true,
          icon:'List'
        }
      }
    ],
    meta: {
      title: '订单管理',
      showLink:true,

    }
  },

  // 活动管理
  {
    path: '/activity',
    component: LayOut,
    redirect: '/activity/activityinit',
    children: [
      {
        path: '/activity/activityinit',
        name: 'ActivityInit',
        component: () => import('@/views/ActivityInit.vue'),
        meta: {
          title: '活动配置',
          showLink:true
        }
      },
      {
        path: '/activity/activitycreate',
        name: 'ActivityCreate',
        component: () => import('@/views/ActivityCreate.vue'),
        meta: {
          title: '活动创建',
          showLink:true
        }
      }

    ],
    meta: {
      title: '活动管理',
      showLink:true,
      icon:'Management'
    }
  },
  // 决策引擎

  {
    path: '/enterdata',
    component: LayOut,
    redirect: '/enterdata/datainit',
    children: [

      {
        path: '/enterdata/datainit',
        name: 'DataInit',
        component: () => import('@/views/DataInit.vue'),
        meta: {
          title: '决策配置',
          showLink:true,
          icon:'InfoFilled'
        }
      },
    ],
    meta: {
      title: "决策与引擎",
      showLink:true
    }
  },

  // 登录信息
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta:{
      title:"登录页面",
      showLink:false
    }
  },
  // 错误页面
  {
    path: '/404',
    component: NotFundPage,
    meta:{
      title:'NOT FOUND',
      showLink:false
    }
  },

  // 秒杀商品页面
  {
    path:'/shop',
    component:()=>import('../views/shop/Shop.vue'),
    meta:{
      title:"秒杀商城",
      showLink:false
    }

  }
]

const allowBaseRoutes = ['/404','/login']

const router = createRouter({
  // 创建web哈希页面就是前面有个#
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
    const authorityRoutes = storageSession.getItem('routes')
    document.title = to.meta.title as string
    // 数组不为空且长度不为0说明登录了
    if(authorityRoutes&&authorityRoutes.length !==0){
      const  accessRouteArray = [...allowBaseRoutes,...authorityRoutes]
      accessRouteArray.includes(to.path)?next():next('/')
    }else{
         allowBaseRoutes.includes(to.path)?next():next('/login')
    }
})


export default router
